﻿/*******************************************************************
* Copyright(c)
* 文件名称: PhotoWallControll.cs
* 简要描述:
* 作者: 千喜
* 邮箱: 2470460089@qq.com
******************************************************************/

using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.Serialization;
#if UNITY_EDITOR
using UnityEditor;
#endif

public class PhotoWallControll : MonoBehaviour
{
    [Header("单个相片的预制体")]
    public RectTransform prefab;
    
    [Header("生成相册有多少行多少列")]
    public int row = 10;       // 行
    public int column = 20;        // 列
    
    [Header("第一个相片的初始位置(可以先将预制体的锚点设置为右上角,然后将其挪到想要的位置,)")]
    public int startXPos = 60;
    public int startYPos = -100;
    
    [Header("相片大小 + 边距")]
    public float distanceX = 65;
    public float distanceY = 65;
    [Header("用来决定终点位置,相对于初始位置的X值")]
    public float initMoveDistance = 1200;
    [Header("中间物体放大比例,最好与radiateSize一起调")]
    public float enlargeSize = 5;
    [Header("周围物体扩散范围的半径,一般是enlargeSize * 照片的宽")]
    public float radiateSize = 220;

    List<List<RectTransform>> goList;
    Dictionary<RectTransform, Vector2> itemPosDict;
    List<RectTransform> changedItemList;

    // Use this for initialization
    void Start () {

        goList = new List<List<RectTransform>>();
        itemPosDict = new Dictionary<RectTransform, Vector2>();
        changedItemList = new List<RectTransform>();

        CreateGos();
    }


    void CreateGos()
    {
        // 生成所有物体，并添加到字典
        for (int i = 0; i < row; i++)
        {
            List<RectTransform> gos = new List<RectTransform>();
            goList.Add(gos);
            float lastPosX = 0;
            for (int j = 0; j < column; j++)
            {
                RectTransform item = (Instantiate(prefab.gameObject) as GameObject).GetComponent<RectTransform>();
                item.name = i + " " + j;
                item.transform.SetParent(transform);
                Vector2 startPos = new Vector3(distanceX + lastPosX, startYPos - i * distanceY);
                item.anchoredPosition = startPos;
                Vector2 endPos = new Vector3(startPos.x - initMoveDistance, startYPos - i * distanceY);
                Tweener tweener = item.DOAnchorPosX(endPos.x, Random.Range(1.8f, 2f));  // 缓动到目标位置
                tweener.SetDelay(j * 0.1f + (row - i) * 0.1f);      // 延时
                tweener.SetEase(Ease.InSine);           // 缓动效果
                item.gameObject.SetActive(true);
                gos.Add(item);
                itemPosDict.Add(item, endPos);

                lastPosX = item.anchoredPosition.x;
            }
        }
    }
    
    public void OnMousePointEnter(RectTransform item)
    {
        // 缓动改变中心物体尺寸
        item.DOScale(enlargeSize, 0.5f);

        Vector2 pos = itemPosDict[item];

        changedItemList = new List<RectTransform>();

        // 添加扩散物体到集合
        foreach (KeyValuePair<RectTransform, Vector2> i in itemPosDict)
        {
            if(Vector2.Distance(i.Value, pos) < radiateSize)
            {
                changedItemList.Add(i.Key);
            }
        }

        // 缓动来解决扩散物体的动画
        for (int i = 0; i < changedItemList.Count; i++)
        {
            Vector2 targetPos = itemPosDict[item] + (itemPosDict[changedItemList[i]] - itemPosDict[item]).normalized * radiateSize;
            changedItemList[i].DOAnchorPos(targetPos, 0.8f);
        }
    }
    //https://github.com/googlevr/gvr-unity-sdk/issues/985
    
    public void OnMousePointExit(RectTransform go)
    {
        // 缓动恢复中心物体尺寸
        go.DOScale(1, 1);
        // 缓动将扩散物体恢复到初始位置
        for (int i = 0; i < changedItemList.Count; i++)
        {
            changedItemList[i].DOAnchorPos(itemPosDict[changedItemList[i]], 0.8f);
        }
    }
}